package com.example.lib_cache.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lib_cache.R;
import com.example.lib_cache.databinding.ItemStoryBinding;
import com.example.lib_cache.model.Story;

import java.util.List;

public class StoryAdapter extends RecyclerView.Adapter<StoryAdapter.StoryViewHolder> {
    private List<Story> itemDatas;

    public StoryAdapter(List<Story> itemDatas) {
        this.itemDatas = itemDatas;
    }

    @NonNull
    @Override
    public StoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemStoryBinding itemStoryBinding=
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                        R.layout.item_story,parent,false);

        return new StoryViewHolder(itemStoryBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull StoryViewHolder holder, int position) {
            holder.binding.setItem(itemDatas.get(position));
            holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return null==itemDatas?0:itemDatas.size();
    }

    class StoryViewHolder extends RecyclerView.ViewHolder{
        ItemStoryBinding binding;

        public StoryViewHolder(ItemStoryBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
